<?php 

/**
 * Profile models 
 * 
 * Profile models in console
 *
 * PHP Version: 1.2.3
 * 
 * @author Johari <joharijumali@gmail.com>
 */

/**
 * Class Profile
 * 
 * Class Profile in console models
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Console_Profile extends Eloquent
{

	public static $timestamps = true;
    public static $table = 'sys_user_profile';
    public static $key = 'sup_id';

    public static $rules = array(
		'sup_emel' => 'required|email|unique:sys_user_profile', 
		'sup_no_ic' => 'required|Numeric',
		'sup_nama' => 'required',
		'sup_id_pengguna' => 'required|max:11|Unique:sys_user_profile',
		'wp_tarikh_mula' => 'required',
		'sup_penyelia_satu' => 'required',
		'sup_penyelia_dua' => 'required',
		'wjkp_no_auto' => 'required|not_in:0'
		
    );

	/**
     * Function validate that pass parameter
     *
     * @param varchar $data the varchar to quote
	 *
	 * @return parameter.
     */
	public static function validate($data)
	{
		return Validator::make($data, Static::$rules);
	}
	
	/**
     * Function user
	 *
	 * @return user.
     */
    public function user()
    {
          return $this->belongs_to('Console_User');
    }
	
	/**
     * Function loggedprofile
	 *
	 * @return loggedprofile.
     */
    public static function loggedprofile()
	{
     	$logged_user = Auth::user();

     	$userInfo = Console_Profile::where('su_id', '=', $logged_user->su_id)
                    ->left_join('sys_user_jabatan', 'sys_user_jabatan.suj_no_auto', '=', 'sys_user_profile.suj_no_auto')
                    ->left_join('sys_user_bahagian', 'sys_user_bahagian.sub_no_auto', '=', 'sys_user_profile.sub_no_auto')
                    ->left_join('sys_user_unit', 'sys_user_unit.suu_no_auto', '=', 'sys_user_profile.suu_no_auto')
                    ->left_join('sys_user_kumpulan_pekerja', 'sys_user_kumpulan_pekerja.sukp_no_auto', '=', 'sys_user_profile.sukp_no_auto')
                    ->first(array('sys_user_profile.*', 'suj_jabatan_name', 'sub_bahagian_name', 'suu_unit_name', 'sukp_penerangan', 'sup_is_supervisor'));
        
        $supervisor1 = ($userInfo->sup_penyelia_satu)?Console_User::find($userInfo->sup_penyelia_satu)->profile->sup_nama:'';
        $supervisor2 = ($userInfo->sup_penyelia_dua)?Console_User::find($userInfo->sup_penyelia_dua)->profile->sup_nama:'';

		if($userInfo->sup_is_supervisor == 1){
			$supervisor = 'Ya';
		} else {
			$supervisor = 'Tidak';
		}
		
        $data['sup_nama'] = $userInfo->sup_nama;
        $data['sup_id_pengguna'] = $userInfo->sup_id_pengguna;
        $data['sup_no_ic'] = $userInfo->sup_no_ic;
        $data['sup_no_pekerja'] = $userInfo->sup_no_pekerja;
        $data['sup_emel'] = $userInfo->sup_emel;
        $data['sup_tel'] = $userInfo->sup_tel;
        $data['wp_tarikh_mula'] = ($userInfo->sup_tarikh_mula != '0000-00-00' )?date('d-m-Y', strtotime($userInfo->sup_tarikh_mula)):'';
        $data['wp_tarikh_tamat'] = ($userInfo->sup_tarikh_tamat != '0000-00-00' )?date('d-m-Y', strtotime($userInfo->sup_tarikh_tamat)):'';
        $data['wjkp_no_auto'] = $userInfo->sukp_penerangan;
        $data['wpj_no_auto'] = $userInfo->suj_jabatan_name;
        $data['wpb_no_auto'] = $userInfo->sub_bahagian_name;
        $data['wpu_no_auto'] = $userInfo->suu_unit_name;
        $data['sur_id'] = Console_Role::find($logged_user->sur_id)->sur_name;
        $data['su_status'] = $logged_user->su_status;
        $data['sup_penyelia_satu'] = $supervisor1;
        $data['sup_penyelia_dua'] = $supervisor2;
        $data['sup_tidak_kelihatan'] = $userInfo->sup_tidak_kelihatan;
        $data['sup_is_supervisor'] = $supervisor;

        return json_encode($data);
    }
	
	/**
     * Function listSupervisor that pass parameter
     *
     * @param varchar $jabatan  the varchar to quote
     * @param integer $bahagian the integer to quote
     * @param integer $unit     the integer to quote
     * @param integer $exclude  the integer to quote
	 *
	 * @return parameter.
     */

	public static function listSupervisorOne($jabatan, $bahagian = 0, $unit = 0)
	{
        $listSupervisor = array(Lang::line('global.pleasechoose')->get());
		
        if ($jabatan != 0 && $bahagian != 0 && $unit != 0) {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('sub_no_auto', '=', $bahagian)
										->where('suu_no_auto', '=', $unit)
										->where('su_status', '=', 'Aktif')
										->where('sup_is_supervisor', '=', 1)
										->get();
        } elseif ($jabatan != 0 && $bahagian != 0 && $unit == 0) {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('sub_no_auto', '=', $bahagian)
										->where('suu_no_auto', '=', 0)
										->where('su_status', '=', 'Aktif')
										->where('sup_is_supervisor', '=', 1)
										->get();
        } elseif ($jabatan != 0 && $bahagian == 0 && $unit == 0) {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('sub_no_auto', '=', 0)
										->where('suu_no_auto', '=', 0)
										->where('su_status', '=', 'Aktif')
										->where('sup_is_supervisor', '=', 1)
										->get();
        } else {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('sub_no_auto', '=', 0)
										->where('suu_no_auto', '=', $unit)
										->where('su_status', '=', 'Aktif')
										->where('sup_is_supervisor', '=', 1)
										->get();
        }

        foreach ($supervisor as $key => $value) {
            $listSupervisor[$value->su_id] = $value->sup_nama;
        }

        return $listSupervisor;
    }
	
	public static function listSupervisorTwo($jabatan)
	{
        $listSupervisor = array(Lang::line('global.pleasechoose')->get());
		
        if ($jabatan != 0) {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('su_status', '=', 'Aktif')
										->where('sup_is_supervisor', '=', 1)
										->get();
        } else {
			$supervisor = Console_Profile::left_join('sys_user', 'sys_user_profile.su_id', '=', 'sys_user.su_id')
										->where('suj_no_auto', '=', $jabatan)
										->where('sup_is_supervisor', '=', 1)
										->get();
        }

        foreach ($supervisor as $key => $value) {
            $listSupervisor[$value->su_id] = $value->sup_nama;
        }

        return $listSupervisor;
    }

    /**
     * Function to get Access Control Level for each user
	 * 
	 * @return getStaffIDArray.
     */

	public static function getStaffIDArray($supervisorID){

		$user = Console_User::find($supervisorID);

		$authProfile = $user->profile;

		if($user->sur_id == 1):

			$access = Console_Profile::all(array('sup_id_pengguna','sup_nama'));

		else:

			$access = Console_Profile::where(function($query) use($supervisorID)
						    {
						        $query->where('sup_penyelia_satu', '=',$supervisorID);
						        $query->or_where('sup_penyelia_dua', '=',$supervisorID);
						    })
							->get(array('sup_id_pengguna','sup_nama'));

		endif;


		$data = array('Sila Pilih');

		$data[$authProfile->sup_id_pengguna] = $authProfile->sup_nama.' ('.$authProfile->sup_id_pengguna.')';

		foreach ($access as $value) {
			$data[$value->sup_id_pengguna] = $value->sup_nama.' ('.$value->sup_id_pengguna.')';
		}

		return $data;

	}

}

?>